package learningThread.syn.synchronizedMethodLockObj.lock.condition.simple;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * condition.await();前必须调用 lock.lock()获取对象锁
 * Object中的wait相当于lock中的await
 *           notify相当于condition中的signal
 *           notifyAll相当于condition中的notifyAll
 */
public class MyService {

    private Lock lock = new ReentrantLock();

    private Condition condition = lock.newCondition();

    public void await(){
        try {
            System.out.println("开始进入锁");
            lock.lock();
            System.out.println("即将释放锁");
            condition.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            System.out.println("释放锁完成");
        }
    }

    public void signal(){
        try {
            lock.lock();
            System.out.println("signal时间为:"+System.currentTimeMillis());
            condition.signal();
        } finally {
            lock.unlock();
        }

    }
}
